package cn.itcast.seatademo.bank2.service.impl;

import cn.itcast.seatademo.bank2.dao.AccountInfoDao;
import cn.itcast.seatademo.bank2.service.AccountInfoService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class AccountInfoServiceImpl implements AccountInfoService {
    @Autowired
    AccountInfoDao accountInfoDao;

    /**
     * 扣减账户
     *
     * @param accountNo 账户
     * @param amount    扣减金额
     */
    @Transactional//注意这个也要加
    @Override
    public void updateAccountBalance(String accountNo, Double amount) {

        // RootContext.getXID()
        log.info("bank2 xid:{}", RootContext.getXID());
        //李四增加金额
        accountInfoDao.updateAccountBalance(accountNo, amount);
        //制造异常
        if(amount==2){ throw new RuntimeException("bank2 make exception 2"); }
    }
}
